/**
 *
 */
package com.angel.mocks.io.rowProcessorCommands;

import java.text.DateFormat;
import java.util.Date;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.commands.exports.ExportRowProcessorCommand;
import com.angel.io.processors.commands.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.ExportRowFile;
import com.angel.io.type.rows.ImportRowFile;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.mocks.io.User;

/**
 * @author William
 *
 */
public class UserRowProcessorCommand implements ImportRowProcessorCommand, ExportRowProcessorCommand{

	public static final String ROLE_NAME_COLUMN = "roleName";
	public static final String USER_NAME_COLUMN = "userName";
	public static final String USER_PASSWORD_COLUMN = "userPassword";
	public static final String DATE_BORNED_COLUMN = "dateBorned";
	public static final String SURNAME_COLUMN = "surname";
	public static final String AGE_COLUMN = "age";
	public static final String NAME_COLUMN = "name";

    public String[] getHeaderColumnsNames() {
        return new String[]{
        		USER_NAME_COLUMN,
        		USER_PASSWORD_COLUMN,
        		ROLE_NAME_COLUMN,
        		DATE_BORNED_COLUMN,
        		SURNAME_COLUMN, 
        		AGE_COLUMN,
        		NAME_COLUMN
        		};
    }

	public void checkRowData(ImportRowFile rowFile, HeaderRowFile header) throws InvalidRowDataException {
		String roleName = rowFile.getStringValue(header.getColumnPosition(ROLE_NAME_COLUMN));
		String userName = rowFile.getStringValue(header.getColumnPosition(USER_NAME_COLUMN));
		String userPassword = rowFile.getStringValue(header.getColumnPosition(USER_PASSWORD_COLUMN));
		String age = rowFile.getStringValue(header.getColumnPosition(AGE_COLUMN));
		String name = rowFile.getStringValue(header.getColumnPosition(NAME_COLUMN));
		String surName = rowFile.getStringValue(header.getColumnPosition(SURNAME_COLUMN));

		boolean areAllNotEmpty = StringHelper.areAllNotEmpty(roleName, userName, userPassword, age, name, surName);
		
		Date dateBorned = rowFile.getDateValue(DateFormat.getDateInstance(DateFormat.SHORT), header.getColumnPosition(DATE_BORNED_COLUMN));

		if(!areAllNotEmpty){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"RoleName: [" + roleName + "] - " +
					"UserName: [" + userName + "] - " +
					"Age: [" + age + "] - " +
					"UserPassword: [" + userPassword + "] - " +
					"DateBorned: [" + dateBorned + "].");
		}
	}

	@SuppressWarnings("unchecked")
	public User processRow(ImportRowFile rowFile, HeaderRowFile header, ParametersService parametersServices) {

		String userName = rowFile.getStringValue(header.getColumnPosition(USER_NAME_COLUMN));
		String userPassword = rowFile.getStringValue(header.getColumnPosition(USER_PASSWORD_COLUMN));
		Date dateBorned = rowFile.getDateValue(DateFormat.getDateInstance(DateFormat.SHORT), header.getColumnPosition(DATE_BORNED_COLUMN));
		Integer age = rowFile.getIntegerValue(header.getColumnPosition(AGE_COLUMN));
		String name = rowFile.getStringValue(header.getColumnPosition(NAME_COLUMN));
		String surName = rowFile.getStringValue(header.getColumnPosition(SURNAME_COLUMN));
		String roleName = rowFile.getStringValue(header.getColumnPosition(ROLE_NAME_COLUMN));

		User user = new User();
		user.setDateBorned(dateBorned);
		user.setAge(age);
		user.setName(name);
		user.setRoleName(roleName);
		user.setSurname(surName);
		user.setUserName(userName);
		user.setUserPassword(userPassword);

		return user;
	}

	public void processRow(ExportRowFile exportRowFile, Object toExport, HeaderRowFile headerRowFile) throws InvalidRowDataException {
		User userToExport = (User) toExport;
		exportRowFile.addStringValue(userToExport.getUserName(), headerRowFile.getColumnPosition(USER_NAME_COLUMN));
		exportRowFile.addStringValue(userToExport.getUserPassword(), headerRowFile.getColumnPosition(USER_PASSWORD_COLUMN));
		exportRowFile.addStringValue(userToExport.getSurname(), headerRowFile.getColumnPosition(SURNAME_COLUMN));
		exportRowFile.addDateValue(userToExport.getDateBorned(), DateFormat.getDateInstance(DateFormat.MEDIUM), headerRowFile.getColumnPosition(DATE_BORNED_COLUMN));
		exportRowFile.addStringValue(userToExport.getName(), headerRowFile.getColumnPosition(NAME_COLUMN));
		exportRowFile.addIntegerValue(userToExport.getAge(), headerRowFile.getColumnPosition(AGE_COLUMN));
		exportRowFile.addStringValue(userToExport.getRoleName(), headerRowFile.getColumnPosition(ROLE_NAME_COLUMN));
	}

	public void checkHeader(HeaderRowFile headerRowFile) throws InvalidRowDataException {
		if(!headerRowFile.hasAllColumnName(this.getHeaderColumnsNames())){
			String[] columnsNotContained = headerRowFile.getColumnsNotContained(this.getHeaderColumnsNames());
			throw new InvalidRowDataException("Some row data are NULL:" + StringHelper.convertToPlainString(columnsNotContained,","));
		}
	}


}
